import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useLocalStorage } from '@vueuse/core'
import { userGetInfoService } from '@/api/user.js'

export const useUserStore = defineStore('yjy-user', () => {
  //这里使用 useLocalStorage 来持久化保存至浏览器
  const token = useLocalStorage('hmtoken', '')
  const setToken = (newToken) => {
    token.value = newToken
  }

  const removeToken = () => {
    token.value = ''
  }

  const user = ref({})
  const fetchUser = async () => {
    const res = await userGetInfoService()
    user.value = res.data.data
  }

  const setUser = (obj) => {
    user.value = obj
  }

  return {
    token,
    setToken,
    removeToken,
    user,
    fetchUser,
    setUser,
  }
})